package com.yuju.order.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.PageModel;
import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.order.dao.IRoomOrderChangeDao;
import com.yuju.order.dao.entity.RoomOrderChange;
import com.yuju.order.model.RoomOrderChangeAccountModel;
import com.yuju.order.model.RoomOrderChangeModel;
import com.yuju.order.service.IRoomOrderChangeAccountService;
import com.yuju.order.service.IRoomOrderChangeService;
import com.yuju.util.Utils;

/**
 * 
 * 类名：RoomOrderChangeServiceImpl  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2018年1月3日 上午10:42:22  <br />
 * @version : 1.1 <br />
 */
@Service
public class RoomOrderChangeServiceImpl extends AbstractBaseService<RoomOrderChangeModel, RoomOrderChange, String> implements IRoomOrderChangeService{

	@Autowired
	private IRoomOrderChangeDao roomOrderChangeDao;
	@Autowired
	private IRoomOrderChangeAccountService roomOrderChangeAccountService;
	
	@Override
	public IBaseDao<RoomOrderChangeModel, RoomOrderChange, String> getDao() {
		// TODO Auto-generated method stub
		return roomOrderChangeDao;
	}
	/**
	 * 
	 * 功能：列表方法<br/>
	 *
	 * @author zhousc
	 * @Date 2018年1月4日 上午9:43:48 <br/>
	 */
	@Override
	public PageModel changeOrderList(RoomOrderChangeModel model, Integer currentPage, Integer rows) {
		PageModel pageModel = getDao().list(model, currentPage, rows);
		
		if(Utils.isNotNull(pageModel)){
			List<RoomOrderChangeModel> orderChangeList = (List<RoomOrderChangeModel>) pageModel.getRows();
			//如果结果不为空。，查询对应改单信息的账单明细
			for(int i = 0; i < orderChangeList.size(); i++){
				RoomOrderChangeModel selectAccountByChangeOrderId = roomOrderChangeDao.selectAccountByChangeOrderId(orderChangeList.get(i).getId());
				if(Utils.isNotNull(selectAccountByChangeOrderId)){
					//房费
					orderChangeList.get(i).setRoomRates(selectAccountByChangeOrderId.getRoomRates());
					//消费
					orderChangeList.get(i).setConsume(selectAccountByChangeOrderId.getConsume());
					//结算
					orderChangeList.get(i).setGathering(selectAccountByChangeOrderId.getGathering());
				}
			}
		}
		return pageModel;
	}
	
	/**
	 * 
	 * 功能: <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2018年1月3日 上午10:43:30<br/>
	 * 
	 * @param roomOrderChangeModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#updateSelective(java.lang.Object)
	 */
	@Override
	public int updateSelective(RoomOrderChangeModel roomOrderChangeModel){
		roomOrderChangeModel.setGmtModified(new Date());
		return this.getDao().updateSelective(roomOrderChangeModel);
	}

	/**
	 * 
	 * 功能:新增改单 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2018年1月3日 下午2:54:47<br/>
	 * 
	 * @param convert
	 * @param roomOrderChangeAccountModels
	 * @return <br/>
	 * @see com.yuju.order.service.IRoomOrderChangeService#saveRoomOrderChange(com.yuju.order.model.RoomOrderChangeModel, java.util.List)
	 */
	@Override
	public int saveRoomOrderChange(RoomOrderChangeModel roomOrderChangeModel,
			List<RoomOrderChangeAccountModel> roomOrderChangeAccountModels) {
		
		roomOrderChangeModel.setId(Utils.getUUID());
		roomOrderChangeModel.setGmtCreate(new Date());
		roomOrderChangeModel.setGmtModified(new Date());
		this.getDao().saveSelective(roomOrderChangeModel);
		//添加账务明细
		for (int i = 0; i < roomOrderChangeAccountModels.size(); i++) {
			roomOrderChangeAccountModels.get(i).setOrderChangeId(roomOrderChangeModel.getId());
			roomOrderChangeAccountService.save(roomOrderChangeAccountModels.get(i));
		}
		
		return 1;
	}
}